#ifndef UDP_PROCESSOR
#define UDP_PROCESSOR

#include <string>
#include <iostream>
#include <stdexcept>
#include <netinet/in.h>
#include "constants.h"
#include <errno.h>
#include <sys/types.h>
#include <sys/socket.h>
#include "packetBuffer.h"
#include "IPv6Address.h"

class UDPMessage
{
	public:
		UDPMessage();
		virtual ~UDPMessage();

		void setSource( const IPv6Address &sourceAddress );
		void setDestination( const IPv6Address &destinationAddress );
		void setData( const PacketBuffer &packetData );
		void setData( const uint8_t* buffer, const int &size );
		IPv6Address source() const;
		IPv6Address destination() const;
		//std::string source() const;
		//std::string destination() const;
		PacketBuffer data() const;

	private:
		IPv6Address sourceAddress_;
		IPv6Address destinationAddress_;
		PacketBuffer packet_;
};

/* \class UDPServer
 * This class handles incomming UDP connections.
 */

class UDPServer
{
	public:
		UDPServer( const std::string port = "0", const std::string listenAddress = "any", const bool ipv6=true );
		virtual ~UDPServer();

		UDPMessage incommingMessage(); //blocks until receving a message
		void setPortNum( const uint16_t &portnum );
		void setPortNum( const std::string &portnum );
		void setListenAddress( const std::string &listenAddress );
		std::string portNum( );
		std::string listenAddress( );

	private:
		void printError() const;
		void createSocket();


		struct sockaddr_in6 listenAddress_;
		int socket_;
		bool bound_;
};

class UDPClient
{

};

class UDPError: public std::runtime_error
{
	public:
		UDPError(std::string message):std::runtime_error(message) {}
};

#endif
